from flask import Flask, current_app

import qingdeng.orms
from com.utils import setup_log
from config import config
from extensions import register_plugin
from qingdeng.api import register_api
from qingdeng.views import register_blueprint


def create_app(config_name):
    app = Flask("qingdeng_bbs")
    app.config.from_object(config[config_name])
    setup_log(config_name)
    register_plugin(app)
    register_blueprint(app)
    register_api(app)

    # 排查错误代码
    @app.cli.command()
    def search():
        from qingdeng.orms import ArticleORM

        # 具体的代码
        paginate = ArticleORM.query.paginate(page=1, per_page=10, error_out=False)
        print(paginate.items)

    return app
